package com.warmheart.core.pool;

import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.warmheart.core.ex.ServerException;

/**
 * 
 * @ClassName: PoolTaskUtil
 * @Description: 多线程执行工具类
 * @author ZhangJianyang
 * @date 2025-06-16 09:37:02
 *
 */
public class PoolSupplierUtil {

    protected PoolSupplierUtil() {
    }

    public static void waitFor(PoolResult<?>... poolResults) throws ServerException {
        List<PoolResult<?>> poolResultList = new ArrayList<PoolResult<?>>();
        if (poolResults != null && poolResults.length > 0) {
            for (PoolResult<?> currPoolResult : poolResults) {
                poolResultList.add(currPoolResult);
            }
        }
        PoolOper.custom(poolResultList, null).build().waitFor();
    }

    /**
     * 
     * @Title: waitFor
     * @Description: 执行逻辑并等待执行完成（该方法会进行阻塞等待，该方法完成后，所有线程逻辑均执行完毕）
     * @author ZhangJianyang
     * @return
     * @throws ServerException
     */
    public static void waitFor(@NotNull ThreadPoolTaskExecutor threadPoolTaskExecutor, PoolResult<?>... poolResults)
            throws ServerException {
        List<PoolResult<?>> poolResultList = new ArrayList<PoolResult<?>>();
        if (poolResults != null && poolResults.length > 0) {
            for (PoolResult<?> currPoolResult : poolResults) {
                poolResultList.add(currPoolResult);
            }
        }
        PoolOper.custom(poolResultList, threadPoolTaskExecutor).build().waitFor();
    }

}
